﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using yc.Dal;

namespace yc.Site.Controllers
{
	public class ClientController : Controller
	{
		public ActionResult Index()
		{
			var db = new DBContext();
			var ProvinceSysNo = -1;
			if (!string.IsNullOrEmpty(Request["ProvinceSysNo"])) ProvinceSysNo = int.Parse(Request["ProvinceSysNo"]);
			ViewBag.Gender = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Gender")).OrderBy(p => p.Sort), "Worth", "Caption", Request["Gender"]));
			ViewBag.Ethnic = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Ethnic")).OrderBy(p => p.Sort), "Worth", "Caption", Request["Ethnic"]));
			ViewBag.ProvinceSysNo = Common.UnSelect.Union(new SelectList(Common.SysArea.Where(p => p.ProvinceSysNo == 0).OrderBy(p => p.ProvinceName).ToList(), "ID", "ProvinceName", ProvinceSysNo));
			ViewBag.CitySysNo = Common.UnSelect.Union(new SelectList(Common.SysArea.Where(p => p.ProvinceSysNo > 0 && p.ProvinceSysNo == ProvinceSysNo && p.CitySysNo == 0).OrderBy(p => p.CityName).ToList(), "ID", "CityName", Request["CitySysNo"]));

			var pager = db.SysClient/*.Where(p => !p.Blocked)*/.ToPager("CreateDate DESC");
			return View(pager);
		}

		public ActionResult Edit(int Id = 0)
		{
			var db = new DBContext();
			var item = new SysClient();
			if (Id != 0) item = db.SysClient.Find(Id);
			ViewBag.Gender = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Gender")).OrderBy(p => p.Sort), "Worth", "Caption", item.Gender));
			ViewBag.Ethnic = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Ethnic")).OrderBy(p => p.Sort), "Worth", "Caption", item.Ethnic));
			ViewBag.Education = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Education")).OrderBy(p => p.Sort), "Worth", "Caption", item.Education));
			ViewBag.Marriage = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Marriage")).OrderBy(p => p.Sort), "Worth", "Caption", item.Marriage));
			ViewBag.Vision = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Vision")).OrderBy(p => p.Sort), "Worth", "Caption", item.Vision));
			ViewBag.Overtime = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Overtime")).OrderBy(p => p.Sort), "Worth", "Caption", item.Overtime));
			ViewBag.WayOfWorking = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("WayOfWorking")).OrderBy(p => p.Sort), "Worth", "Caption", item.WayOfWorking));
			ViewBag.WayOfRelaxing = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("WayOfRelaxing")).OrderBy(p => p.Sort), "Worth", "Caption", item.WayOfRelaxing));
			ViewBag.WorkStatue = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("WorkStatue")).OrderBy(p => p.Sort), "Worth", "Caption", item.WorkStatue));
			ViewBag.WorkExpress = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("WorkExpress")).OrderBy(p => p.Sort), "Worth", "Caption", item.WorkExpress));
			ViewBag.EnglishLvl = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("EnglishLvl")).OrderBy(p => p.Sort), "Worth", "Caption", item.EnglishLvl));
			ViewBag.ComputerLvl = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("ComputerLvl")).OrderBy(p => p.Sort), "Worth", "Caption", item.ComputerLvl));

			ViewBag.ProvinceSysNo = Common.UnSelect.Union(new SelectList(Common.SysArea.Where(p => p.ProvinceSysNo == 0).OrderBy(p => p.ProvinceName).ToList(), "ID", "ProvinceName", item.ProvinceSysNo));
			ViewBag.CitySysNo = Common.UnSelect.Union(new SelectList(Common.SysArea.Where(p => p.ProvinceSysNo > 0 && p.ProvinceSysNo == item.ProvinceSysNo && p.CitySysNo == 0).OrderBy(p => p.CityName).ToList(), "ID", "CityName", item.CitySysNo));
			return View(item);
		}

		[HttpPost, ActionName("Edit")]
		public ActionResult PostEdit(int Id = 0)
		{
			var db = new DBContext();
			db.Configuration.AutoDetectChangesEnabled = true;
			var item = new SysClient { CreateDate = DateTime.Now };
			if (Id != 0) item = db.SysClient.Find(Id);
			if (TryUpdateModel(item))
			{
				if (Id == 0) db.SysClient.Add(item);
				db.SaveChanges();
				ModelState.AddModelError("", "保存成功");
			}

			ViewBag.Gender = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Gender")).OrderBy(p => p.Sort), "Worth", "Caption", item.Gender));
			ViewBag.Ethnic = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Ethnic")).OrderBy(p => p.Sort), "Worth", "Caption", item.Ethnic));
			ViewBag.Education = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Education")).OrderBy(p => p.Sort), "Worth", "Caption", item.Education));
			ViewBag.Marriage = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Marriage")).OrderBy(p => p.Sort), "Worth", "Caption", item.Marriage));
			ViewBag.Vision = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Vision")).OrderBy(p => p.Sort), "Worth", "Caption", item.Vision));
			ViewBag.Overtime = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("Overtime")).OrderBy(p => p.Sort), "Worth", "Caption", item.Overtime));
			ViewBag.WayOfWorking = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("WayOfWorking")).OrderBy(p => p.Sort), "Worth", "Caption", item.WayOfWorking));
			ViewBag.WayOfRelaxing = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("WayOfRelaxing")).OrderBy(p => p.Sort), "Worth", "Caption", item.WayOfRelaxing));
			ViewBag.WorkStatue = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("WorkStatue")).OrderBy(p => p.Sort), "Worth", "Caption", item.WorkStatue));
			ViewBag.WorkExpress = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("WorkExpress")).OrderBy(p => p.Sort), "Worth", "Caption", item.WorkExpress));
			ViewBag.EnglishLvl = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("EnglishLvl")).OrderBy(p => p.Sort), "Worth", "Caption", item.EnglishLvl));
			ViewBag.ComputerLvl = Common.UnSelect.Union(new SelectList(Common.Dictionary.Where(p => p.Category.Equals("ComputerLvl")).OrderBy(p => p.Sort), "Worth", "Caption", item.ComputerLvl));

			ViewBag.ProvinceSysNo = Common.UnSelect.Union(new SelectList(Common.SysArea.Where(p => p.ProvinceSysNo == 0).OrderBy(p => p.ProvinceName).ToList(), "ID", "ProvinceName", item.ProvinceSysNo));
			ViewBag.CitySysNo = Common.UnSelect.Union(new SelectList(Common.SysArea.Where(p => p.ProvinceSysNo > 0 && p.ProvinceSysNo == item.ProvinceSysNo && p.CitySysNo == 0).OrderBy(p => p.CityName).ToList(), "ID", "CityName", item.CitySysNo));
			return View(item);
		}

		public ActionResult Block(int Id = 0)
		{
			var item = new SysClientBlockHistory { ClientID = Id };
			return View(item);
		}

		[HttpPost, ActionName("Block")]
		public ActionResult PostBlock(SysClientBlockHistory item)
		{
			var db = new DBContext();
			db.Configuration.AutoDetectChangesEnabled = true;
			item.CreateBy = Common.LoginName;
			item.CreateDate = DateTime.Now;
			db.SysClientBlockHistory.Add(item);
			var client = db.SysClient.Find(item.ClientID);
			client.Blocked = true;
			db.SaveChanges();
			ModelState.AddModelError("", "保存成功");
			ViewBag.Saved = 1;
			return View(item);
		}

		#region Working
		public ActionResult EditWorking(int pid = 0, int wid = 0)
		{
			var item = new SysClientWorking { CID = pid };
			var db = new DBContext();
			if (wid != 0) item = db.SysClientWorking.Find(wid);
			return View(item);
		}

		[HttpPost, ActionName("EditWorking")]
		public ActionResult PostEditWorking(int id = 0)
		{
			var db = new DBContext();
			db.Configuration.AutoDetectChangesEnabled = true;
			var item = new SysClientWorking();
			if (id != 0) item = db.SysClientWorking.Find(id);
			if (TryUpdateModel(item))
			{
				if (id == 0) db.SysClientWorking.Add(item);
				db.SaveChanges();
				ModelState.AddModelError("", "保存成功");
				ViewBag.Saved = 1;
			}
			return View(item);
		}

		[HttpPost]
		public ActionResult DelWorking(int wid = 0)
		{
			if (wid == 0) return Json(new { res = false, msg = "数据非法" });
			var db = new DBContext();
			db.Configuration.AutoDetectChangesEnabled = true;
			var item = db.SysClientWorking.Find(wid);
			if (item == null) return Json(new { res = false, msg = "数据非法" });
			db.SysClientWorking.Remove(item);
			db.SaveChanges();
			return Json(new { res = true, msg = "删除成功" });
		}
		#endregion

		#region Training
		public ActionResult EditTraining(int pid = 0, int wid = 0)
		{
			var item = new SysClientTraining { CID = pid };
			var db = new DBContext();
			if (wid != 0) item = db.SysClientTraining.Find(wid);
			return View(item);
		}

		[HttpPost, ActionName("EditTraining")]
		public ActionResult PostEditTraining()
		{
			var db = new DBContext();
			db.Configuration.AutoDetectChangesEnabled = true;
			var item = new SysClientTraining();
			var id = int.Parse("0" + Request.Form["ID"]);
			if (id != 0) item = db.SysClientTraining.Find(id);
			if (TryUpdateModel(item))
			{
				if (id == 0) db.SysClientTraining.Add(item);
				db.SaveChanges();
				ModelState.AddModelError("", "保存成功");
				ViewBag.Saved = 1;
			}
			return View(item);
		}

		[HttpPost]
		public ActionResult DelTraining(int wid = 0)
		{
			if (wid == 0) return Json(new { res = false, msg = "数据非法" });
			var db = new DBContext();
			db.Configuration.AutoDetectChangesEnabled = true;
			var item = db.SysClientTraining.Find(wid);
			if (item == null) return Json(new { res = false, msg = "数据非法" });
			db.SysClientTraining.Remove(item);
			db.SaveChanges();
			return Json(new { res = true, msg = "删除成功" });
		}
		#endregion

		public ActionResult Print(int Id)
		{
			var db = new DBContext();
			var item = db.SysClient.Find(Id);

			ViewBag.Gender = Common.Dictionary.Where(p => p.Category.Equals("Gender") && p.Worth.Equals(item.Gender)).Select(p => p.Caption).FirstOrDefault();
			ViewBag.Ethnic = Common.Dictionary.Where(p => p.Category.Equals("Ethnic") && p.Worth.Equals(item.Ethnic)).Select(p => p.Caption).FirstOrDefault();
			ViewBag.Education = Common.Dictionary.Where(p => p.Category.Equals("Education") && p.Worth.Equals(item.Education)).Select(p => p.Caption).FirstOrDefault();
			ViewBag.Marriage = Common.Dictionary.Where(p => p.Category.Equals("Marriage") && p.Worth.Equals(item.Marriage)).Select(p => p.Caption).FirstOrDefault();
			ViewBag.Vision = Common.Dictionary.Where(p => p.Category.Equals("Vision") && p.Worth.Equals(item.Vision)).Select(p => p.Caption).FirstOrDefault();
			ViewBag.Overtime = Common.Dictionary.Where(p => p.Category.Equals("Overtime") && p.Worth.Equals(item.Overtime)).Select(p => p.Caption).FirstOrDefault();
			ViewBag.WayOfWorking = Common.Dictionary.Where(p => p.Category.Equals("WayOfWorking") && p.Worth.Equals(item.WayOfWorking)).Select(p => p.Caption).FirstOrDefault();
			ViewBag.WayOfRelaxing = Common.Dictionary.Where(p => p.Category.Equals("WayOfRelaxing") && p.Worth.Equals(item.WayOfRelaxing)).Select(p => p.Caption).FirstOrDefault();
			ViewBag.WorkStatue = Common.Dictionary.Where(p => p.Category.Equals("WorkStatue") && p.Worth.Equals(item.WorkStatue)).Select(p => p.Caption).FirstOrDefault();
			ViewBag.WorkExpress = Common.Dictionary.Where(p => p.Category.Equals("WorkExpress") && p.Worth.Equals(item.WorkExpress)).Select(p => p.Caption).FirstOrDefault();
			ViewBag.EnglishLvl = Common.Dictionary.Where(p => p.Category.Equals("EnglishLvl") && p.Worth.Equals(item.EnglishLvl)).Select(p => p.Caption).FirstOrDefault();
			ViewBag.ComputerLvl = Common.Dictionary.Where(p => p.Category.Equals("ComputerLvl") && p.Worth.Equals(item.ComputerLvl)).Select(p => p.Caption).FirstOrDefault();

			ViewBag.ProvinceSysNo = Common.SysArea.Where(p => p.ID.Equals(item.ProvinceSysNo)).Select(p => p.ProvinceName).FirstOrDefault();
			ViewBag.CitySysNo = Common.SysArea.Where(p => p.ProvinceSysNo.Equals(item.ProvinceSysNo) && p.CitySysNo.Equals(item.CitySysNo)).Select(p => p.CityName).FirstOrDefault();
			return View(item);
		}

		public ActionResult PCity(int provinceSysNo)
		{
			var db = new DBContext();
			var citys = db.SysArea
				.Where(p =>
					p.ProvinceSysNo > 0 &&
					p.ProvinceSysNo == provinceSysNo &&
					p.CitySysNo == 0)
				.OrderBy(p => p.CityName)
				.ToList();
			return View(citys);
		}

		public ActionResult ValidateClient(string Name, DateTime Birthday)
		{
			var db = new DBContext();
			var clients = db.SysClient.Where(p =>
				p.Name.Equals(Name) &&
				p.Birthday == Birthday
			);
			return View(clients.ToList());
		}
	}
}

